﻿var InsertCalculatorHotel = function (hotelTypeItem, nationalItem, cityItem, hotelItem, tourItem, roomItem,
    dateBeginItem, dateEndItem, quantityAdultItem, quantityChildItem, priceAdultItem, priceChildItem,
    vatItem, totalPriceHotel, countNightItem , seasonTour, tourInfo  ) {

    var loadPriceRoom = function () { 
        var roomId = $(roomItem).val(); 
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "GetPrice", "priceId": roomId },
            dataType: "json",
            success: function (data) { 
                if (data && data.isError == false) {
                    $(priceAdultItem).val(data.PriceAdult);
                    $(priceChildItem).val(data.PriceChild);
                }
            }
        });
    };
    $(roomItem).change(loadPriceRoom);
    
    $('#btnInsertHotel').live("click", function () {
         
        var typeId = 5;
        var tourId = parseInt(tourItem);
        //room =PriceId
        var roomId = $(roomItem).val();
        var dateBegin = $(dateBeginItem).val();
        var dateEnd = $(dateEndItem).val();
        var quantityAdult = $(quantityAdultItem).val();
        var quantityChild = $(quantityChildItem).val();
        var priceAdult = $(priceAdultItem).val();
        var priceChild = $(priceChildItem).val();
        var vat = $(vatItem).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        if (dateBegin != NaN && dateEnd != NaN && quantityAdult > 0 && priceAdult > 0 && vat >=0 && roomId>0) {
                $.ajax({
                    type: "POST",
                    url: "/ajax.html",
                    data: { "function": "InsertCalculatorHotel", "TypeId": typeId, "TourId": tourId, "RoomId": roomId, "DateBegin": dateBegin, "DateEnd": dateEnd, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "PriceAdult": priceAdult, "PriceChild": priceChild, "VAT": vat ,"CalculatorId":calculatorId,"ClassName":className},
                    dataType: "json",
                    success: function (data) {
                        $(".ListHotel").remove();
                        $(hotelTypeItem).val('-1');
                        $(nationalItem).val('-1');
                        $(cityItem).val('-1');
                        $(roomItem).val('-1');
                        $(dateBeginItem).val('');
                        $(dateEndItem).val('');
                        $(quantityAdultItem).val('');
                        $(quantityChildItem).val('');
                        $(priceAdultItem).val('');
                        $(priceChildItem).val('');
                        $(vatItem).val('');
                        $(countNightItem).text('');
                        $(totalPriceHotel).val('');
                        if (data && data.isError == false) {
                            $.each(data.array, function (index, item) { 
                                $('#new-ticket-Hotel')
                                    .before($("<tr class='ListHotel' id='ticket-hotel-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                        '<td >' + item.HotelType + '</td>' +
                                        '<td >' + item.National + '</td>' +
                                        '<td >' + item.City + '</td>' +
                                        '<td >' + item.Hotel + '</td>' +
                                        '<td >' + item.DateBegin + '</td>' +
                                        '<td >' + item.DateEnd + '</td>' +
                                        '<td>' + item.CountNight + '</td>' +
                                        '<td >' + item.RoomType + '</td>' +
                                        '<td >' + item.PriceAdult + '</td>' +
                                        '<td >' + item.PriceChild + '</td>' +
                                        '<td >' + item.QuantityAdult + '</td>' +
                                        '<td >' + item.QuantityChild + '</td>' +
                                        '<td >' + item.VAT + '</td>' +
                                        '<td>' + item.Total +
                                        '<td>' +
                                        '<input class="btnUpdateHotel"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                        '<input class="btnDeleteHotel"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                        '</td>'));
                                             });
                            $('#TotalTicketHotelPrices').html(data.TotalTicketHotelPrices);
                            $('#TotalTicketHotelPricesAdults').html(data.TotalTicketHotelPricesAdults);
                            $('#TotalTicketHotelPricesChild').html(data.TotalTicketHotelPricesChild);

                            $('#btnInsertHotel').attr("calculatorid", "-1") .val("Thêm");
                            $('.btnUpdateHotel').show();
                            $('#btnDeleteHotel').hide();
                        }
                    }
                });
            }
    });
    $('.btnDeleteHotel').live("click", function () {
      
        var calculatorId = $(this).attr('calculatorid');
        var typeId = 5;
        var tourId = parseInt(tourItem);
        //room =PriceId
        var roomId = $(roomItem).val();
        var dateBegin = $(dateBeginItem).val();
        var dateEnd = $(dateEndItem).val();
        var quantityAdult = $(quantityAdultItem).val();
        var quantityChild = $(quantityChildItem).val();
        var priceAdult = $(priceAdultItem).val();
        var priceChild = $(priceChildItem).val();
        var vat = $(vatItem).val(); 
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "InsertCalculatorHotel", "TypeId": typeId, "TourId": tourId, "RoomId": roomId, "DateBegin": dateBegin, "DateEnd": dateEnd, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "PriceAdult": priceAdult, "PriceChild": priceChild, "VAT": vat, "CalculatorId": calculatorId, "ClassName": className },
            dataType: "json",
            success: function(data) {
               if (data && data.isError == false) {
                   $.each(data.array, function (index, item) {
                       $(".ListHotel").remove();
                       $('#new-ticket-Hotel')
                                    .before($("<tr class='ListHotel' id='ticket-hotel-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                        '<td >' + item.HotelType + '</td>' +
                                        '<td >' + item.National + '</td>' +
                                        '<td >' + item.City + '</td>' +
                                        '<td >' + item.Hotel + '</td>' +
                                        '<td >' + item.DateBegin + '</td>' +
                                        '<td >' + item.DateEnd + '</td>' +
                                        '<td>' + item.CountNight + '</td>' +
                                        '<td >' + item.RoomType + '</td>' +
                                        '<td >' + item.PriceAdult + '</td>' +
                                        '<td >' + item.PriceChild + '</td>' +
                                        '<td >' + item.QuantityAdult + '</td>' +
                                        '<td >' + item.QuantityChild + '</td>' +
                                        '<td >' + item.VAT + '</td>' +
                                        '<td>' + item.Total +
                                        '<td>' +
                                        '<input class="btnUpdateHotel"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                        '<input class="btnDeleteHotel"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                        '</td>'));
                    });
                    $('#TotalTicketHotelPrices').html(data.TotalTicketHotelPrices);
                    $('#TotalTicketHotelPricesAdults').html(data.TotalTicketHotelPricesAdults);
                    $('#TotalTicketHotelPricesChild').html(data.TotalTicketHotelPricesChild); 
                 
                    $('#btnInsertHotel').attr("calculatorid", "-1").val("Thêm");
                    $('.btnUpdateHotel').show();
                    $('#btnDeleteHotel').hide();

                }
            }
        });
    });

    $('.btnUpdateHotel').live('click', function() {
        var calculatorId = $(this).attr("calculatorid");
        
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "UpdateCalculatorHotel", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#ticket-hotel-' + calculatorId).remove();  
                $(hotelTypeItem).val(data.HotelTypeId);
                $(nationalItem).val(data.Nationalid);
                loadCityNationalid(data.CityId, data.HotelTypeId, data.HotelId, data.PriceId, seasonTour);
                $(dateBeginItem).val(data.BeginDate);
                $(dateEndItem).val(data.EndDate);
                $(quantityAdultItem).val(data.QuantityAdults);
                $(quantityChildItem).val(data.QuantityChild);
                $(priceAdultItem).val(data.PriceAdults);
                $(priceChildItem).val(data.PriceChild);
                $(vatItem).val(data.Vat);
                $(countNightItem).text(fncCountNightauto(data.BeginDate, data.EndDate)); 
                $(totalPriceHotel).text(data.TotalPriceMenu);  
                $('#btnInsertHotel').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateHotel').hide();
                $('#btnDeleteHotel').show();
            }
        });
    });
    //load city
    var loadCityNationalid  = function (cityItemId,hotelTypeId  ,hotelId,  priceId ,  season) {
        var nationalId = $(nationalItem).val(); 
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "ListCityByNational", "NationalId": nationalId },
            dataType: "json",
            success: function (data) {
                $(cityItem).html('<option value="-1">--- Chọn tỉnh/thành phố ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(cityItem)
                            .append($("<option></option>")
                                .attr("value", item.ID)
                                .text(item.Name));
                    });
                    $(cityItem).val(cityItemId);
                    fncHotel(cityItemId, hotelTypeId, hotelId, priceId, season);
                } 
            }
        }); 
    };
    //load Hotel
    var fncHotel = function (cityItemId, hotelTypeId, hotelId, priceId, season) { 
        if (hotelTypeId > 0 && cityItemId > 0) {
            $.ajax({
                type: "POST",
                url: "/ajax.html",
                data: { "function": "ListHotelByHotelType", "HotelTypeId": hotelTypeId, "CityId": cityItemId },
                dataType: "json",
                success: function(data) {
                    $(hotelItem).html('<option value="-1">--- Chọn khách sạn---</option>');
                    if (data && data.isError == false) {
                        $.each(data.array, function(index, item) {
                            $(hotelItem)
                                .append($("<option></option>")
                                    .attr("value", item.ID)
                                    .text(item.Name));
                        });
                        $(hotelItem).val(hotelId);
                        loadRoomTypeByHotel(hotelId, priceId, season);
                    }
                }
            });
        }
    };
    //Load loai
    var loadRoomTypeByHotel = function (hotelId, priceId, season) {
          $.ajax({
                type: "POST",
                url: "/ajax.html",
                data: { "function": "ListRoomTypeByHotel", "HotelId": hotelId, "Season": season },
                dataType: "json",
                success: function (data) {
                    $(roomItem).html('<option value="-1">--- Chọn loại phòng---</option>');
                    if (data && data.isError == false) {
                        $.each(data.array, function (index, item) {
                            $(roomItem)
                                .append($("<option></option>")
                                    .attr("value", item.ID)
                                    .text(item.Name));
                        });
                        $(roomItem).val(priceId);
                    }
                }
            }); 
    };
    var diffDays = 0;
    var fncCountNight = function () {
        if ($(dateBeginItem).val() != "" && $(dateEndItem).val() != "") {
            var dateBegin = $(dateBeginItem).val().split("/");
            var dateEnd = $(dateEndItem).val().split("/");
            var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
            var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
            var oneDay = 24 * 60 * 60 * 1000;
            diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay);
            if (diffDays == 0) {
                diffDays = 1;}
            $(countNightItem).text(diffDays);
        }
    };
    $(dateBeginItem).change(fncCountNight);
    $(dateEndItem).change(fncCountNight);
    var fncCountNightauto = function (dateBeginauto, dateEndauto) {
        if ($(dateBeginItem).val() != "" && $(dateEndItem).val() != "") {
            var dateBegin = dateBeginauto.split("/");
            var dateEnd = dateEndauto.split("/");
            var firstDate = new Date(dateBegin[2], dateBegin[1], dateBegin[0]);
            var secondDate = new Date(dateEnd[2], dateEnd[1], dateEnd[0]);
            var oneDay = 24 * 60 * 60 * 1000;
            diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay);
            if (diffDays == 0) {
                diffDays = 1;
            }
            $(countNightItem).text(diffDays);
        }
    };
    var fncTotalPriceRoom = function( ) {
        var quantityAdult = $(quantityAdultItem).val();
        var quantityChild = $(quantityChildItem).val();
        var priceAdult = $(priceAdultItem).val();
        var priceChild = $(priceChildItem).val();
        var vat = $(vatItem).val();
        if (vat.toString() == NaN) vat = 0;
        var total = ((quantityAdult * priceAdult) + (quantityChild * priceChild)+((quantityAdult * priceAdult) + (quantityChild * priceChild)) * vat / 100)*diffDays;
        $(totalPriceHotel).text(total);
    };
    $(quantityAdultItem).keyup(fncTotalPriceRoom);
    $(quantityChildItem).keyup(fncTotalPriceRoom);
    $(priceAdultItem).keyup(fncTotalPriceRoom);
    $(priceChildItem).keyup(fncTotalPriceRoom);
    $(vatItem).keyup(fncTotalPriceRoom);
    $(dateBeginItem).change(fncTotalPriceRoom);
    $(dateEndItem).change(fncTotalPriceRoom);
};